package com.itheima.tlias84.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias84.dto.ReportStuDegreeDTO;
import com.itheima.tlias84.mapper.StudentMapper;
import com.itheima.tlias84.service.ReportServiceStu;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ReportServiceStuImpl implements ReportServiceStu {

    @Resource
    StudentMapper studentMapper;
    @Override
    public List<ReportStuDegreeDTO> countStuByDegree() {
        return studentMapper.countStuByDegree();
    }

    @Override
    public ResultUtil countStuByClazzId() {
        List<Integer> clazzIds = studentMapper.countStuByClazzId();
        Map<String,List<Integer>> map = clazzIds.stream().collect(Collectors.groupingBy(t->{
            return switch (t){
                case 1 ->"Java";
                case 4 ->"基础不会班";
                case 5 ->"进阶班1";
                case 6 ->"前端1";
                case 7 ->"进阶班";
                case 8 ->"前端2";
                case 9 ->"Java1";
                case 10 ->"python";
                case 11 ->"大数据";
                case 13 ->"前端3";
                default -> "其他";
            };
        }));
        Set<String> clazzList = map.keySet();
        List<Integer> dataList = map.values().stream().map(t->t.size()).toList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("clazzList", clazzList);
        jsonObject.put("dataList", dataList);
        return ResultUtil.success(jsonObject);
    }
}
